package cn.zl.ZLM.event.callback.impl;

import cn.zl.ZLM.event.callback.inter.IMKNoFoundCallBack;
import cn.zl.ZLM.helper.ZLMApiHelper;
import cn.zl.ZLM.structure.MK_MEDIA_INFO;
import cn.zl.ZLM.structure.MK_SOCK_INFO;
import cn.yunbamboos.Const;
import cn.zl.common.ZLConst;
import cn.zl.common.manage.StreamProxyManage;
import cn.zl.common.manage.VirtualVideoStreamManage;
import cn.yunbamboos.mybatis.conditions.Conditions;
import cn.zl.config.ZLConfig;
import cn.zl.mapper.StreamProxyMapper;
import cn.zl.mapper.VirtualVideoStreamMapper;
import cn.zl.model.StreamProxy;
import cn.zl.model.VirtualVideoStream;
import com.sun.jna.CallbackThreadInitializer;
import com.sun.jna.Native;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class MKNoFoundCallBack implements IMKNoFoundCallBack {

    private static final Logger logger = LoggerFactory.getLogger(MKNoFoundCallBack.class);

    @Resource
    private ZLConfig zlConfig;
    @Resource
    private VirtualVideoStreamMapper virtualVideoStreamMapper;
    @Resource
    private VirtualVideoStreamManage virtualVideoStreamManage;
    @Resource
    private StreamProxyMapper streamProxyMapper;
    @Resource
    private StreamProxyManage streamProxyManage;

    public MKNoFoundCallBack() {
        Native.setCallbackThreadInitializer(this, new CallbackThreadInitializer(true, false, "MediaNoFoundThread"));
    }

    /**
     * 未找到流后会广播该事件，请在监听该事件后去拉流或其他方式产生流，这样就能按需拉流了
     *
     * @param url_info 播放url相关信息
     * @param sender   播放客户端相关信息
     * @return 1 直接关闭
     * 0 等待流注册
     */
    @Override
    public int invoke(MK_MEDIA_INFO url_info, MK_SOCK_INFO sender) {
        //通过ZLM_API.mk_media_info_get_*可以查询流的各种信息
        String app = ZLMApiHelper.getInstance().getApi().mk_media_info_get_app(url_info);
        String stream = ZLMApiHelper.getInstance().getApi().mk_media_info_get_stream(url_info);
        String schema = ZLMApiHelper.getInstance().getApi().mk_media_info_get_schema(url_info);
        String params = ZLMApiHelper.getInstance().getApi().mk_media_info_get_params(url_info);
        String host = ZLMApiHelper.getInstance().getApi().mk_media_info_get_host(url_info);
        int port = ZLMApiHelper.getInstance().getApi().mk_media_info_get_port(url_info);
        logger.info("未找到 schema:{} app:{} stream:{} host:{} port:{} param: {}", schema, app, stream, host, port, params);

        // 查询是否在模拟流
        if(ZLConst.VIRTUAL_VIDEO.APP.equals(app)){
            Optional<VirtualVideoStream> optional = virtualVideoStreamMapper.queryForObject(Conditions.create("app", app).add("stream", stream));
            if (optional.isPresent()) {
                // 开启模拟视频推流
                VirtualVideoStream virtualVideoStream = optional.get();
                virtualVideoStreamManage.start(virtualVideoStream);
                return 0;
            }
        }
        // 查询是否存在拉流代理
        Optional<StreamProxy> optional = streamProxyMapper.queryForObject(Conditions.create("app", app).add("stream", stream));
        if(optional.isPresent()){
            // 开启拉流
            StreamProxy streamProxy = optional.get();
            streamProxyManage.start(streamProxy);
            return 0;
        }
        // 0 等待流注册 1 直接关闭
        return 1;
    }

}